﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SHJunketingBLL;
using SHJunketingModel;

public partial class Admin_UserManagement_EditRoleInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack) 
        {
            GetCbListRole();
            if (Request.QueryString["roleId"] != null) 
            {
                int roleId = Convert.ToInt32(Request.QueryString["roleId"]);
                ViewState["roleId"] = roleId;

                C_UserRoleInfo userRoleInfo = C_UserRoleInfoManager.GetC_UserRoleInfoByRoleId(roleId);
                this.txtName.Text = userRoleInfo.RoleName;
                this.txtDesc.Text = userRoleInfo.RoleDesc;

                string permission = userRoleInfo.Permissions;
                string sa = permission.Insert(0, "|");
                foreach (ListItem li in this.cbPer.Items)
                {
                    int index = sa.IndexOf("|" + li.Value + "|");
                    if (index != -1) 
                    {
                        li.Selected = true;
                    }
                }
            } 
        }
    }

    //绑定CheckBoxList
    private void GetCbListRole()
    {
        IList<C_Permission> list = C_PermissionManage.GetAllC_Permissions();
        this.cbPer.DataSource = list;
        this.cbPer.DataTextField = "PermissionsName";
        this.cbPer.DataValueField = "PermissionsID";
        this.cbPer.DataBind();
    }
    //修改
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (txtDesc.Text.Length < 100)
        {

            string permission = "";
            string roleName = this.txtName.Text;
            int roleId = Convert.ToInt32(ViewState["roleId"]);

            foreach (ListItem li in this.cbPer.Items)
            {
                if (li.Selected == true)
                {
                    permission += li.Value + "|";
                }
            }
            C_UserRoleInfo roleInfo = new C_UserRoleInfo();
            roleInfo.RoleId = roleId;
            roleInfo.RoleName = roleName;
            roleInfo.Permissions = permission;

            string desc = this.txtDesc.Text.Trim();

            roleInfo.RoleDesc = desc;

            C_UserRoleInfoManager.ModifyC_UserRoleInfo(roleInfo);

            Web.CommFun.Show_Msg("修改成功！", "UserRoleInfos.aspx");
        }
        else 
        {
            Web.CommFun.Show_Msg("角色说明过长！");
        }
    }
}
